﻿// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Oliver Rau" email="mail@olira.de"/>
//     <version>$Revision: 3 $</version>
//     <created>12.08.2006 21:31</created>
// </file>

using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.Core;

namespace ns2kml
{
	/// <summary>
	/// Description of WiScanFileHelper.
	/// </summary>
	public class WiScanFileHelper
	{
		public static List<APInfo> GetInfoFromFile(string fileName)
		{
			LoggingService.DebugFormatted("WiScanFileHelper started: {0}", fileName);
			
			StreamReader sr = new StreamReader(fileName);
			
			string line;
			
			string dateGMT = "";
			
			string token;
			int pos;
			
			string lat, lon, ssid, bssid, time;
			string[] snrArray;
			
			APInfo info;
			
			List<APInfo> apInfo = new List<APInfo>();
			
			while ((line = sr.ReadLine()) != null) {
				if (line.StartsWith("#")) {
					token = "$DateGMT: ";
					pos = line.IndexOf(token);
					
					if (pos > -1) {
						dateGMT = line.Substring(pos + token.Length);
					}
					
					continue;
				}
				
				info = new APInfo();
				
				// # Latitude	Longitude	( SSID )	Type	( BSSID )	Time (GMT)	[ SNR Sig Noise ]	# ( Name )	Flags	Channelbits	BcnIntvl	DataRate	LastChannel
				string[] values = line.Split('\t');
				
				// --> Latitude: (S -> N -)
				lat = values[0];
				
				if (lat.StartsWith("N"))
					lat = lat.Replace("N", "").Trim();
				else
					lat = "-" + lat.Replace("S", "").Trim();
				
				info.BestLat = Convert.ToDouble(lat, System.Globalization.CultureInfo.InvariantCulture);
				// <-- Latitude: (S -> N -)
				
				// --> Longitude: (W -> E -)
				lon = values[1];
				
				if (lon.StartsWith("E"))
					lon = lon.Replace("E", "").Trim();
				else
					lon = "-" + lon.Replace("W", "").Trim();
				
				info.BestLong = Convert.ToDouble(lon, System.Globalization.CultureInfo.InvariantCulture);
				// <-- Longitude: (W -> E -)
				
				// --> SSID: ( ( SSID ) -> SSID )
				ssid = values[2];
				ssid = ssid.Substring(2, ssid.Length - 4);
				
				info.SSID = ssid;
				// <-- SSID: ( ( SSID ) -> SSID )
				
				// not used node.Type = values[3];
				
				// --> BSSID: ( ( BSSID ) -> BSSID )
				bssid = values[4];
				bssid = bssid.Substring(2, bssid.Length - 4);
				
				info.BSSID = bssid;
				// <-- BSSID: ( ( BSSID ) -> BSSID )
				
				// --> Time
				time = values[5].Split(' ')[0];
				info.LastSeen = Convert.ToDateTime(dateGMT + " " + time);
				// <-- Time
				
				// --> SNR ( [ SNR Sig Noise ] -> SNR Sig Noise )
				snrArray = values[6].Substring(2, values[6].Length - 4).Split(' ');
				info.MaxSNR    = int.Parse(snrArray[0]);
				info.MaxSignal = int.Parse(snrArray[1]);
				info.MaxNoise  = int.Parse(snrArray[2]);
				// <-- SNR ( [ SNR Sig Noise ] -> SNR Sig Noise )
				
				
				// Flags
				info.Flags = (WifiFlag)UInt16.Parse(values[8]);
				
				// Channel
				info.LastChannel = Byte.Parse(values[12]);
				
				LoggingService.DebugFormatted("BSSID: {0}", info.BSSID);
				LoggingService.DebugFormatted("LastSeen:  {0}", info.LastSeen.ToLocalTime());
				LoggingService.Debug("----");
				
				apInfo.Add(info);
			}
			
			sr.Close();
			return apInfo;
		}
	}
}
